﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DAL.Gateway
{
    internal class BaseDA:DAL.Gateway.Conexion,IDisposable
    {
        public BaseDA()
        {
            DAL.Gateway.BaseDA.IniciarSesion(Config.ConnectionString, Config.DBTYPE);
        }
        internal DAL.Gateway.Conn GetConn(string Guid)
        {
            try
            {
                DataTable dt = new DataTable();
                Conn conn = null;

                string FunctionName = "DATAFRAMEWORK.GetConnectionParametersByGUID";
                string DTName = string.Empty;
                
                dt = DAL.Gateway.BaseDA.GDatos.TraerDataTable(FunctionName, Guid);

                if (dt.Rows.Count > 0)
                {
                    conn = new Conn()
                    {
                        ConnectionString = dt.Rows[0].Field<string>("sConnectionString"),
                        SPName = dt.Rows[0].Field<string>("sProcedureName"),
                        DBType = this.StringToEDBMS(dt.Rows[0].Field<string>("sConnectorName"))
                    };
                }

                return conn;
            }
            catch (Exception ex)
            {
                throw new Exception("BaseDA - GetDataTransactionName - ", ex);
            }
        }
        private DAL.Gateway.EDBMS StringToEDBMS(string dbType)
        {
            DAL.Gateway.EDBMS dbtype = Gateway.EDBMS.SqlServer;

            switch (dbType)
            {
                case "FIREBIRD":
                    dbtype = Gateway.EDBMS.Firebird;
                    break;
                case "MYSQL":
                    dbtype = Gateway.EDBMS.MySql;
                    break;
                case "ORACLE":
                    dbtype = Gateway.EDBMS.Oracle;
                    break;
                case "POSTGRESQL":
                    dbtype = Gateway.EDBMS.PostgreSQL;
                    break;
                case "MSACCESS":
                    dbtype = Gateway.EDBMS.MSAccess;
                    break;
                case "HSQLDB":
                    dbtype = Gateway.EDBMS.HSQLDB;
                    break;
                default:
                    dbtype = Gateway.EDBMS.SqlServer;
                    break;
            }
            return dbtype;
        }
        public void Dispose()
        {
            DAL.Gateway.BaseDA.GDatos.CerrarConexion();
        }
    }
}
